<template>
  <div class="box">
    <div class="top">
      <el-button size="medium" @click="open()">{{ title }}
        <i :class="icon"></i>
      </el-button>

      <div class="right">
        <div v-for="(item, i) in rightTag" :key="item.id" :style="title === item.name ? 'color:#ec4141' : 'color:#000'"
          @click="changeTag(item, i)">
          {{ item.name }}
        </div>
      </div>
    </div>

    <div class="tagBox" v-if="icon !== 'el-icon-arrow-right'">
      <div class="tag" @click="def()" :style="id === null ? 'color:#ec4141' : 'color:#000'">
        全部视频
      </div>
      <div v-for="(item, i) in tagData" :key="item.id" class="tag"
        :style="title === item.name ? 'color:#ec4141' : 'color:#000'" @click="changeTag(item, i)">
        {{ item.name }}
      </div>
    </div>

    <div class="content" v-if="videoData !== null">
      <div v-for="item in videoData" :key="item.type === 1 ? item.data.vid : item.data.id" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.type === 1
                  ? item.data.playTime >= 10000
                    ? (item.data.playTime / 10000).toFixed(0) + "万"
                    : item.data.playTime
                  : item.data.playCount >= 10000
                    ? (item.data.playCount / 10000).toFixed(0) + "万"
                    : item.data.playCount
            }}
          </div>
          <el-image :src="item.data.coverUrl" lazy fit="cover" @click="playMv(item)">
            <div slot="placeholder" class="image-slot">
              <el-image src="" lazy @click="playMv(item)"></el-image>
            </div>
          </el-image>
          <div class="time">
            {{
                Math.floor(
                  item.type === 1
                    ? item.data.durationms / 1000
                    : item.data.duration / 1000
                ) | timeFormat
            }}
          </div>
          <div style="padding: 14px">
            <el-tooltip class="item" effect="dark" :content="item.type === 1 ? item.data.title : item.data.name"
              placement="top">
              <div class="desc">
                {{ item.type === 1 ? item.data.title : item.data.name }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
    <div class="more" @click="load()" v-if="videoData !== null">
      换一批 <i class="el-icon-arrow-down"></i>
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  mounted() {
    if (localStorage.getItem('cookie') !== null) {
      this.getCookie = this.cookie;
      this.getRec();
      this.getRight();
    } else {
      this.$message.error('需要登录');
    }
    this.getGroup();
  },
  computed: {
    ...mapState(['cookie']),
  },
  beforeRouteLeave(to, from, next) {
    this.loading = true
    next()
  },
  watch: {
    cookie(newVal, oldVal) {
      this.getCookie = '';
      this.getCookie = newVal;
      this.getRec();
      this.getRight();
    },
  },
  data() {
    return {
      tagData: null,
      icon: 'el-icon-arrow-right',
      title: '全部视频',
      id: null,
      videoData: null,
      offset: 0,
      nowTag: null,
      getCookie: null,
      loading: false,
      rightTag: null,
    };
  },
  methods: {
    ...mapActions([
      'saveMVurl',
      'saveResMv',
      'saveMvBes',
      'saveArtistsItem',
      'saveVurl',
      'saveResv',
      'saveVbes',
    ]),
    // 获取标签分类
    async getGroup() {
      this.$api.gropuList().then((ret) => {
        this.tagData = ret.data;
      });
    },

    // 获取右侧标签
    async getRight() {
      this.$api.categoryList({ cookie: this.getCookie }).then((ret) => {
        this.rightTag = ret.data;
      });
    },

    // 打开分类
    open() {
      if (this.icon === 'el-icon-arrow-right') {
        this.icon = 'el-icon-arrow-down';
      } else if (this.icon === 'el-icon-arrow-down') {
        this.icon = 'el-icon-arrow-right';
      }
    },

    // 更换标签
    changeTag(item, i) {
      this.title = item.name;
      this.id = i;
      this.nowTag = item.id;
      this.getVideoList();
    },

    // 获取
    async getVideoList() {
      if (localStorage.getItem('cookie') !== null) {
        this.getCookie = localStorage.getItem('cookie');
        this.$api
          .videoGroup({
            id: this.nowTag,
            cookie: this.getCookie,
            offset: this.offset,
          })
          .then((ret) => {
            this.$message({
              message: ret.msg,
              type: 'success',
            });
            this.videoData = ret.datas;
            this.icon = 'el-icon-arrow-right';
          });
      } else {
        this.$message.error('需要登录');
      }
    },

    // 获取推荐视频
    async getRec() {
      this.$api
        .recVideo({ cookie: this.getCookie, offset: this.offset })
        .then((ret) => {
          this.videoData = ret.datas;
          this.$message({
            message: ret.msg,
            type: 'success',
          });
        });
    },

    // 换一批
    load() {
      this.offset += 1;
      if (this.nowTag === null) {
        this.getRec();
      } else {
        this.getVideoList();
      }
    },

    // 点击播放以后的后续操作
    playMv(e) {
      this.loading = true;
      if (e.type === 1) {
        this.getUrl(e.data.vid);
      } else {
        this.getArtists(e.data.artists[0].id);
        this.getUrl(e.data.id);
      }
    },

    // 获取播放地址
    async getUrl(e) {
      if (this.title === 'MV') {
        this.$api.mvUrl({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveMVurl({ mvUrl: ret.data.url });
        });
      } else {
        this.$api.videoUrl({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveVurl({ vUrl: ret.urls[0].url });
        });
      }
      this.getResMv(e);
      this.getItem(e);
    },

    // 获取相似MV或者视频
    async getResMv(e) {
      if (this.title === 'MV') {
        this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveResMv({ resMv: ret });
        });
      } else {
        this.$api.allvideo({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveResv({ resV: ret.data });
        });
      }
    },

    // 获取MV作者,视频模式下不需要获取
    async getArtists(e) {
      if (this.title === 'MV') {
        window.sessionStorage.setItem('artists', e);
        this.$api.arDetail({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveArtistsItem({ artistsItem: ret.data });
        });
      }
    },

    // 获取MV基础信息
    async getItem(e) {
      if (this.title === 'MV') {
        this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
          this.saveMvBes({ mvBes: ret });
          this.$router.push(`/mvPlay?id=${e}`);
        });
      } else {
        this.$api.videoDetail({ id: e, timestamp: this.$utils.timestamp(), cookie: this.getCookie }).then((ret) => {
          this.saveVbes({ vBes: ret.data });
          console.log(ret.data);
          this.$router.push(`/videoPage?id=${e}`);
        });
      }
    },

    // 默认选中
    def() {
      this.videoData = null;
      this.title = '全部视频';
      this.id = null;
      this.nowTag = null;
      this.offset = 0;
      this.getRec();
      this.icon = 'el-icon-arrow-right';
    },
  },
};
</script>

<style scoped>
.box {
  margin-bottom: 40px;
}

.el-button {
  margin-top: 20px;
  border-radius: 20px !important;
  margin-bottom: 15px;
  height: 40px;
}

.tagBox {
  position: absolute;
  width: 720px;
  height: 200px;
  border: 1px solid #dcdfe6;
  overflow-y: scroll;
  border-radius: 20px;
  display: flex;
  flex-wrap: wrap;
  padding-top: 20px;
  padding-bottom: 20px;
  padding-left: 30px;
  z-index: 11;
  background: #ffffff;
}

.tag {
  width: 80px;
  cursor: pointer;
  margin-bottom: 20px;
  margin-right: 20px;
  font-size: 16px;
  display: flex;
  justify-content: left;
}

.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
  position: relative;
}

.count {
  position: absolute;
  z-index: 10;
  width: 100%;
  text-align: right;
  color: #fff;
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 300px;
  position: relative;
}

.el-card .el-image {
  width: 300px;
  height: 200px;
}

.el-row {
  margin-bottom: 40px;
}

.time {
  position: absolute;
  width: 100%;
  text-align: right;
  color: #fff;
  top: 180px;
}

.more {
  margin-top: 20px;
  margin-bottom: 20px;
  cursor: pointer;
  text-align: center;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.right {
  display: flex;
}

.right div {
  margin-right: 5px;
  cursor: pointer;
}
</style>
